package castledefense.logic.buildings;

import java.util.ArrayList;

import castledefense.data.BuildingData;
import castledefense.data.GameData;
import castledefense.game.screens.MainGameScreen;
import castledefense.logic.Map;
import castledefense.logic.Targetable;

public class Tower2 extends ElementBuilding {

	protected int timeBetweenAttack = 1 * 1500;
	protected int timeBeforeNextAttack = timeBetweenAttack;
	protected int timeAttacking = 500;
	protected int timeBeforeAttackEnds = 0;
	protected double range = 6;
	protected double damage = 2;
	protected boolean attacking;
	protected ArrayList<Targetable> targetList = new ArrayList<Targetable>(4);
	private Map map;

	public Tower2(Map map, int posX, int posY, int type) {
		super(map, posX, posY, 2, type);
		size = 2;
		cost = BuildingData.tower2Cost;
		elementType = GameData.noElement;
	}

	public double getDamage() {
		return damage;
	}

	public int getRange() {
		return (int) range;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public void update(int deltaTime) {
		timeBeforeNextAttack -= deltaTime;
		if (timeBeforeNextAttack < 0) {
			timeBeforeNextAttack = timeBetweenAttack;
			attack();
			attacking = true;
			timeBeforeAttackEnds = timeAttacking;
		}

		timeBeforeAttackEnds -= deltaTime;
		if (timeBeforeAttackEnds < 0)
			attacking = false;
	}

	private void attack() {
		targetList.clear();
		targetList.addAll(map.getNearUnits(getCenterX(), getCenterY(), range,
				BuildingData.tower2TargetMax));
		final int size = targetList.size();
		for (int i = 0; i < size; i++) {
			Targetable target = targetList.get(i);
			int tmpLvl = 0;
			if (elementType != -1) {
				tmpLvl = upgradeLevel[elementType];
			}
			MainGameScreen.missileSystem.createMissile(target, getCenterX(),
					getCenterY(), damage, elementType, tmpLvl);

		}
	}
}
